package com.secondary.dao;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

public class BaseDao implements Dao {
	
	@Resource
	private SessionFactory sessionFactory;

	@Override
	public Object findObject(Class clzz, Serializable id) {
		return getSession().get(clzz, id);
		
	}
	
	@Override
	public List findByProperty(Class clzz, String property, Object value) {
		
		return getSession().createCriteria(clzz).add(Restrictions.eq(property, value)).list();
	}

	@Override
	public List findObjects(Class clzz) {
		return getSession().createCriteria(clzz).list();
	
	}

	@Override
	public void removeObject(Class clzz, Serializable id) {
		// TODO Auto-generated method stub
		getSession().delete(getSession().get(clzz, id));

	}
	public Object findObjectByProperty(Class clzz, String property, Object value) {
		
		return getSession().createCriteria(clzz).add(Restrictions.eq(property, value)).uniqueResult();
	}

	
	@Override
	public void saveObject(Object o) {
		// TODO Auto-generated method stub
		getSession().save(o);
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	protected Session getSession() {
		System.out.println(sessionFactory.getCurrentSession());
		 return sessionFactory.getCurrentSession();
	}

}
